<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('work_related_deaths', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 关联工伤记录（如果是先工伤后工亡的情况）
            $table->unsignedBigInteger('work_injury_record_id')->nullable();
            $table->string('injury_number', 50)->nullable();

            // 员工信息
            $table->unsignedBigInteger('employee_id');
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();
            $table->string('id_card_number', 18)->nullable();
            $table->date('birth_date')->nullable();
            $table->integer('age_at_death')->nullable();

            // 工亡基本信息
            $table->string('death_number', 50)->unique(); // 工亡编号
            $table->datetime('death_time'); // 死亡时间
            $table->string('death_location', 200); // 死亡地点
            $table->string('death_type', 50); // 工亡类型：先工伤后工亡、直接工亡
            $table->text('death_cause'); // 死亡原因
            $table->text('death_process'); // 死亡经过

            // 工作信息
            $table->unsignedBigInteger('department_id')->nullable();
            $table->string('department_name', 100)->nullable();
            $table->unsignedBigInteger('position_id')->nullable();
            $table->string('position_name', 100)->nullable();
            $table->date('hire_date')->nullable(); // 入职日期
            $table->decimal('monthly_salary', 10, 2)->nullable(); // 月工资
            $table->integer('service_years')->nullable(); // 工作年限

            // 认定信息
            $table->datetime('confirmation_time')->nullable(); // 认定时间
            $table->string('confirmation_result', 50)->nullable(); // 认定结果
            $table->string('confirmation_number', 100)->nullable(); // 认定文号
            $table->string('confirmation_authority', 200)->nullable(); // 认定机构

            // 责任认定
            $table->string('responsibility_type', 50)->nullable(); // 责任类型
            $table->decimal('responsibility_percentage', 5, 2)->nullable(); // 责任比例
            $table->text('responsibility_analysis')->nullable(); // 责任分析

            // 家属信息
            $table->json('beneficiaries'); // 受益人信息数组
            $table->string('primary_beneficiary_name', 100)->nullable(); // 主要受益人
            $table->string('primary_beneficiary_relationship', 50)->nullable(); // 与死者关系
            $table->string('primary_beneficiary_contact', 100)->nullable(); // 联系方式
            $table->string('primary_beneficiary_id_card', 18)->nullable(); // 身份证号

            // 补助信息
            $table->decimal('funeral_allowance', 10, 2)->default(0); // 丧葬补助
            $table->decimal('living_allowance', 10, 2)->default(0); // 生活补助
            $table->decimal('education_allowance', 10, 2)->default(0); // 教育补助
            $table->decimal('disability_allowance', 10, 2)->default(0); // 伤残补助
            $table->decimal('total_compensation', 12, 2)->default(0); // 总补偿金额

            // 补助来源
            $table->string('compensation_source', 50); // company, insurance, mixed
            $table->decimal('company_contribution', 12, 2)->default(0); // 公司承担金额
            $table->decimal('insurance_contribution', 12, 2)->default(0); // 保险承担金额
            $table->string('insurance_company', 100)->nullable(); // 保险公司
            $table->string('insurance_policy_number', 100)->nullable(); // 保险单号

            // 发放记录
            $table->json('payment_records')->nullable(); // 发放记录数组
            $table->boolean('all_payments_completed')->default(false); // 是否全部发放完毕
            $table->datetime('final_payment_date')->nullable(); // 最后发放日期

            // 状态管理
            $table->string('status', 20)->default('reported'); // reported, confirmed, processing, completed
            $table->boolean('is_closed')->default(false);
            $table->datetime('closed_at')->nullable();
            $table->text('closing_notes')->nullable();

            // 备注和附件
            $table->text('notes')->nullable();
            $table->json('attachments')->nullable(); // 死亡证明、认定书、赔偿协议等

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['employee_id', 'death_time'], 'wrd_emp_dtime_idx');
            $table->index(['death_time'], 'wrd_dtime_idx');
            $table->index(['death_type'], 'wrd_dtype_idx');
            $table->index(['status'], 'wrd_status_idx');
            $table->index(['department_id'], 'wrd_dept_idx');
            $table->index(['is_closed'], 'wrd_closed_idx');
            $table->index(['death_number'], 'wrd_dnumber_idx');

            // 外键约束
            $table->foreign('work_injury_record_id')->references('id')->on('work_injury_records')->onDelete('set null');
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('work_related_deaths');
    }
};
